package com.lvmama.pet.sync.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.lvmama.comm.bee.service.sync.SyncElasticsearchService;
import com.lvmama.comm.sync.po.SyncElasticsearch;
import com.lvmama.pet.sync.dao.SyncElasticsearchMapper;

public class SyncElasticsearchServiceImpl implements SyncElasticsearchService, ApplicationListener<ContextRefreshedEvent>{
	
	@Autowired
	private SyncElasticsearchMapper syncElasticsearchMapper;
	
	public static List<SyncElasticsearch> syncElasticsearchInit = new ArrayList<SyncElasticsearch>();
	
	private static final ExecutorService exec=Executors.newCachedThreadPool();

	
	public void init(){
		List<SyncElasticsearch> list = syncElasticsearchMapper.findAll();
		if(null!=list&&list.size()>0){
			for (SyncElasticsearch syncElasticsearch : list) {
				syncElasticsearchInit.add(syncElasticsearch);
			}
		}
	}

	@Override
	public void save(SyncElasticsearch syncElasticsearch) {
		syncElasticsearchMapper.save(syncElasticsearch);
		syncElasticsearchInit.clear();
		init();
	}

	@Override
	public void update(SyncElasticsearch syncElasticsearch) {
		syncElasticsearchMapper.update(syncElasticsearch);
		syncElasticsearchInit.clear();
		init();

	}

	@Override
	public List<SyncElasticsearch> findAll() {
		return syncElasticsearchMapper.findAll();
	}

	@Override
	public Long count(Map<String, Object> parmas) {
		return syncElasticsearchMapper.count(parmas);
	}

	@Override
	public SyncElasticsearch findById(Long id) {
		return syncElasticsearchMapper.findById(id);
	}

	@Override
	public void deleteById(Long id) {

		syncElasticsearchMapper.deleteById(id);
		syncElasticsearchInit.remove(0);

	}

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext"))//root application context 没有parent，他就是老大.
        {  
            //需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。  
		    exec.submit(new SyncESRunner());
		    //exec.shutdown();

        }
		
	}

	private class SyncESRunner implements Runnable{

		@Override
		public void run() {
			while(true){

				try {
						if(syncElasticsearchInit.size()>0){
							SyncElasticsearch syncElasticsearch = syncElasticsearchMapper.findAll().get(0);
							SyncElasticsearch _syncElasticsearch = syncElasticsearchInit.get(0);
							if(!syncElasticsearch.getIpPort().equals(_syncElasticsearch.getIpPort())||!syncElasticsearch.getClusterName().equals(_syncElasticsearch.getClusterName())){
								syncElasticsearchInit.remove(0);
								syncElasticsearchInit.add(syncElasticsearch);
							}
						}else{
							init();
						}
						Thread.sleep(6000);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			
		}
		
	}

	@Override
	public SyncElasticsearch findByEsCode(String esCode) {
		return syncElasticsearchMapper.findByEsCode(esCode);
	}

	@Override
	public List<SyncElasticsearch> findPageListByCondition(
			Map<String, Object> parmas) {
		return syncElasticsearchMapper.findPageListByCondition(parmas);
	}
}
